<template>
  <div class="app-container">
    <!-- 搜索区域 -->
    <div class="search-area">
      <el-form :model="queryParams" ref="queryForm" :inline="true" class="demo-form-inline">
        <el-form-item label="物资名称" prop="materialName">
          <el-input
            v-model="queryParams.materialName"
            placeholder="请输入"
            clearable
            style="width: 200px;"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleQuery">查询</el-button>
          <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 操作按钮区域 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
    </el-row>

    <!-- 表格数据区域 -->
    <el-table
      v-loading="loading"
      :data="enterpriseList"
      @selection-change="handleSelectionChange"
      border
      style="width: 100%"
    >
      <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column prop="materialName" label="物资名称" min-width="120"></el-table-column>
        <el-table-column prop="enterpriseName" label="生产企业" min-width="150"></el-table-column>
        <el-table-column prop="area" label="行政区划" min-width="120"></el-table-column>
        <el-table-column prop="address" label="地址" min-width="150"></el-table-column>
        <el-table-column prop="contact" label="负责人" min-width="100"></el-table-column>
        <el-table-column prop="phone" label="联系电话" min-width="120"></el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <div class="pagination-area">
      <el-pagination
        v-show="total > 0"
        :current-page="pageNum"
        :page-size="pageSize"
        :total="total"
        :page-sizes="[10, 20, 30, 50]"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      ></el-pagination>
    </div>

    <!-- 添加或修改物资生产企业对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="物资名称" prop="materialName">
              <el-input v-model="form.materialName" placeholder="请输入" />
            </el-form-item>
            <el-form-item label="生产企业" prop="enterpriseName">
              <el-input v-model="form.enterpriseName" placeholder="请输入" />
            </el-form-item>
            <el-form-item label="负责人" prop="contact">
              <el-input v-model="form.contact" placeholder="请输入" />
            </el-form-item>
            <el-form-item label="联系电话" prop="phone">
              <el-input v-model="form.phone" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="行政区划" prop="area">
              <el-select v-model="form.area" placeholder="请选择" clearable style="width: 100%;">
                <el-option label="城北街道" value="城北街道"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="企业地址" prop="address">
              <el-input v-model="form.address" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            :rows="3"
            placeholder="请输入"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">保存</el-button>
        <el-button @click="cancel">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "YingJiWuZi",
  data() {
    return {
      // 遮罩层
      loading: false,
      // 总条数
      total: 0,
      // 当前页数
      pageNum: 1,
      // 每页显示条数
      pageSize: 10,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        materialName: ''
      },
      // 表单参数
      form: {},
      // 表格数据
      enterpriseList: [],
      // 选中项
      selectionList: [],
      // 表单校验
      rules: {
        materialName: [
          { required: true, message: "物资名称不能为空", trigger: "blur" }
        ],
        enterpriseName: [
          { required: true, message: "生产企业不能为空", trigger: "blur" }
        ],
        contact: [
          { required: true, message: "负责人不能为空", trigger: "blur" }
        ],
        phone: [
          { required: true, message: "联系电话不能为空", trigger: "blur" }
        ],
        area: [
          { required: true, message: "行政区划不能为空", trigger: "change" }
        ],
        address: [
          { required: true, message: "企业地址不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询物资生产企业列表 */
    getList() {
      this.loading = true;
      // 模拟数据
      setTimeout(() => {
        this.enterpriseList = [
          {
            id: 1,
            materialName: "防汛沙袋",
            enterpriseName: "江河防护科技有限公司",
            area: "城北街道",
            address: "工业园区防汛路123号",
            contact: "李强",
            phone: "13812345678",
            remark: "支持24小时紧急配送"
          },
          {
            id: 2,
            materialName: "应急移动泵",
            enterpriseName: "水务机械设备制造有限公司",
            area: "城南工业区",
            address: "机械大道88号",
            contact: "王芳",
            phone: "13987654321",
            remark: "备有50台现货，可立即调配"
          },
          {
            id: 3,
            materialName: "水质快速检测设备",
            enterpriseName: "绿源环境科技有限公司",
            area: "高新技术开发区",
            address: "科技路15号环保产业园",
            contact: "陈明",
            phone: "13712345678",
            remark: "含检测试剂，提供技术支持"
          }
        ];
        this.total = this.enterpriseList.length;
        this.loading = false;
      }, 500);
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    
    // 分页处理
    handleSizeChange(val) {
      this.pageSize = val
      this.getList()
    },
    
    handleCurrentChange(val) {
      this.pageNum = val
      this.getList()
    },
    
    // 选择变更
    handleSelectionChange(selection) {
      this.selectionList = selection
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增物资生产企业";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      // 模拟获取详情
      setTimeout(() => {
        this.form = { ...row };
        this.open = true;
        this.title = "修改物资生产企业";
      }, 200);
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            // 模拟修改操作
            setTimeout(() => {
              this.$message.success("修改成功");
              this.open = false;
              this.getList();
            }, 500);
          } else {
            // 模拟新增操作
            setTimeout(() => {
              this.$message.success("新增成功");
              this.open = false;
              this.getList();
            }, 500);
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$confirm('是否确认删除该物资生产企业?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          // 模拟删除操作
          setTimeout(() => {
            this.getList();
            this.$message.success("删除成功");
          }, 500);
        })
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: null,
        materialName: null,
        enterpriseName: null,
        contact: null,
        phone: null,
        area: null,
        address: null,
        remark: null
      };
      this.resetForm("form");
    }
  }
};
</script>

<style scoped>
.app-container {
  padding: 20px;
}

.search-area {
  background-color: #f5f7fa;
  border-radius: 4px;
  /*margin-bottom: 15px;
  padding: 15px;*/
}

.demo-form-inline {
  display: flex;
  align-items: center;
  gap: 10px;
}

.button-area {
  margin-bottom: 15px;
}

.button-area .el-button {
  margin-right: 8px;
}

.el-table {
  border-radius: 4px;
  overflow: hidden;
}

.el-table__header-wrapper th {
  background-color: #f5f7fa !important;
  font-weight: 600;
}

.pagination-area {
  margin-top: 20px;
  padding: 15px 0;
  border-top: 1px solid #ebeef5;
  display: flex;
  justify-content: flex-end;
}

.dialog-footer {
  text-align: right;
  padding: 10px 20px;
  box-sizing: border-box;
}

.mb8 {
  margin-bottom: 8px;
}

.small-padding {
  padding: 8px;
}

.fixed-width {
  width: 150px;
}
</style>